package com.woniuxy.store.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.cloud.mall95.log.Log;
import com.woniuxy.coupon.client.CouponManageClient;
import com.woniuxy.coupon.client.CouponQueryClient;
import com.woniuxy.coupon.client.CouponTypeQueryClient;
import com.woniuxy.coupon.client.param.AddCouponParam;
import com.woniuxy.coupon.client.param.ModifyCouponParam;

import com.woniuxy.coupon.client.param.QueryCouponParam;
import com.woniuxy.coupon.client.result.CouponDto;
import com.woniuxy.coupon.client.result.CouponTypeDto;
import com.woniuxy.member.client.MemberQueryClient;
import com.woniuxy.member.client.result.MemberCouponClientDto;
import com.woniuxy.store.app.util.StoreIdUtil;
import com.woniuxy.store.client.CouponsClient;
import com.woniuxy.store.client.param.couponParam.AddCouponParams;
import com.woniuxy.store.client.param.couponParam.ModifyCouponParams;
import com.woniuxy.store.client.param.couponParam.SearchCouponParams;
import exception.MallException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import result.CommonResultCodeEnum;
import result.Result;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/store/marketing")
public class CouponsClientImpl implements CouponsClient {
    @Autowired
    private CouponManageClient couponManageClient;
    @Autowired
    private CouponQueryClient couponQueryClient;
    @Autowired
    private CouponTypeQueryClient couponTypeQueryClient;
    @Autowired
    private MemberQueryClient memberQueryClient;
    @Override
    @Log("添加优惠卷")
    public Result addCoupon(@Valid AddCouponParams param) {
        Integer storeId = StoreIdUtil.getFromToken();
        Result<CouponTypeDto> result = couponTypeQueryClient.queryCouponTypeByTypeId(param.getCouponTypeId());
        if (result.getData()==null) {
            throw new MallException("当前优惠卷类型不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        if (result.getData().getStoreId()!=storeId) {
            throw new MallException("当前优惠卷类型不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        AddCouponParam addCouponParam = BeanUtil.toBean(param, AddCouponParam.class);
        addCouponParam.setStoreId(storeId);
        return couponManageClient.addCoupon(addCouponParam);
    }

    @Override
    @Log("删除优惠卷")
    public Result deleteCoupon(Integer couponId) {
        Integer storeId = StoreIdUtil.getFromToken();
        Result<CouponDto> result = couponQueryClient.queryCouponById(couponId);
        if (result.getData()==null) {
            throw new MallException("当前优惠卷不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        if (result.getData().getStoreId()!=storeId) {
            throw new MallException("当前优惠卷不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        Result<List<MemberCouponClientDto>> memberCouponById = memberQueryClient.getMemberCouponById(couponId);
        if (memberCouponById.getData()==null||memberCouponById.getData().size()!=0) {
            throw new MallException("当前优惠卷还有用户拥有并未使用", CommonResultCodeEnum.INVALID_PARAM);
        }
        Result<List<MemberCouponClientDto>> coupons = memberQueryClient.getMemberCouponById(couponId);
        if (coupons.getData().size()!=0) {
            throw new MallException("当前优惠卷还在兑换列表中", CommonResultCodeEnum.INVALID_PARAM);
        }
        return couponManageClient.deleteCoupon(storeId,couponId);
    }

    @Override
    @Log("修改优惠卷信息")
    public Result modifyCoupon(@Valid ModifyCouponParams param) {
        Integer storeId = StoreIdUtil.getFromToken();
        Result<CouponDto> result = couponQueryClient.queryCouponById(param.getCouponId());
        if (result.getData()==null) {
            throw new MallException("当前优惠卷不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        if (result.getData().getStoreId()!=storeId) {
            throw new MallException("当前优惠卷不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        Result<CouponTypeDto> resultType = couponTypeQueryClient.queryCouponTypeByTypeId(param.getCouponTypeId());
        if (resultType.getData()==null) {
            throw new MallException("当前优惠卷类型不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        if (resultType.getData().getStoreId()!=storeId) {
            throw new MallException("当前优惠卷类型不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        ModifyCouponParam modifyCouponParam = BeanUtil.toBean(param, ModifyCouponParam.class);
        modifyCouponParam.setStoreId(storeId);
        return couponManageClient.modifyCoupon(modifyCouponParam);
    }

    @Override
    @Log("启用关闭优惠卷")
    public Result modifyCouponState(Integer couponId, String couponState) {
        Integer storeId = StoreIdUtil.getFromToken();
        Result<CouponDto> couponDtoResult = couponQueryClient.storeQueryCoupon(couponId);
        if (couponDtoResult.getData()==null) {
            throw new MallException("当前优惠卷不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        if (couponDtoResult.getData().getStoreId()!=storeId) {
            throw new MallException("当前优惠卷不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        return couponManageClient.modifyCouponState(couponId,couponState);
    }

    @Override
    @Log("根据条件查询优惠卷")
    public Result queryCoupon(@Valid SearchCouponParams param) {
        Integer storeId = StoreIdUtil.getFromToken();
        QueryCouponParam queryCouponParam = BeanUtil.toBean(param, QueryCouponParam.class);
        queryCouponParam.setStoreId(storeId);
        return couponQueryClient.queryCoupon(queryCouponParam);
    }

}
